<h2>
  <%= 'Edit Tag'.t %>
</h2>

<%= error_messages_for :tag %>
<%- form_for :tag, @tag, :url => { :action => "update", :id => @tag.name.gsub(/\//, "%2F")}, :html => { :method => :put } do |f| -%>

  <dl id="edit-tag">
    <dt><%= f.label :name, 'Name'.t %></dt>
    <dd><%= f.text_field :name, :disabled => (!logged_in_as_admin? || ['Rating', 'Warning', 'Category'].include?(@tag[:type])) %></dd>

    <dt><%=h 'Works (count)'.t %></dt>
    <dd><%= link_to @tag.taggings_count, tag_path(@tag) %></dd>

    <dt><%= f.label :type, 'Category'.t %></dt>
    <dd>
    <ul class="tag-edit-items">
    <%- (Tag::TYPES - [@tag.class.name, "Ambiguity"]).each do |type| -%>
      <li>
      <%= f.radio_button(:type, type, {:disabled => !logged_in_as_admin?}) %>&nbsp;<%= f.label("type_#{type.downcase}", type) %>
      </li>
    <%- end -%>
    <%- [@tag.class.name, "Ambiguity"].uniq.each do |type| -%>
      <li>
      <%= f.radio_button(:type, type, {:disabled => !( logged_in_as_admin? || Tag::USER_DEFINED.include?(@tag.class.name) )}) %>&nbsp;<%= f.label("type_#{type.downcase}", type) %>
      </li>
    <%- end -%>
    </ul>
    </dd>

    <dt><%= f.label :canonical, 'Canonical'.t %></dt>
    <dd>
    <%= f.check_box("canonical", :disabled => !(logged_in_as_admin? || Tag::USER_DEFINED.include?(@tag.class.name) )) %>&nbsp;<%= "This is the official name for the ".t + @tag.class.name %></dd>

    <%- if @tag.is_a?(Rating)-%>
      <dt><%= f.label :adult, 'Adult'.t %></dt>
      <dd><%= f.check_box("adult", :disabled => !logged_in_as_admin? )%>&nbsp;<%=h 'This tag indicates adult content.'%></dd>
    <%- end %>

    <%- if Tag::USER_DEFINED.include?(@tag.class.name) %>

      <% if @tag.is_a?(Fandom) %>
        <dt><%= f.label :type, 'Media'.t %></dt>
        <dd><%=h 'Add Media:'.t %>&nbsp;<%= select("media", 'media_name', (Media.canonical - @tag.family).collect {|p| [ p.name, p.name ] }, { :include_blank => true }) %><br />
        <%- unless @tag.medias.blank? -%>
          <ul class="tag-edit-items">
          <% @tag.medias.each do |tag| %>
            <li><%= check_box_tag("medias[]", tag.name, true, :id => "tag_media_#{tag.id}") %><%= f.label "media_#{tag.id}",   link_to_edit_tag(tag) %></li>
          <%- end %>
          </ul>
        <%- end %>
        </dd>
      <%- elsif !@tag.is_a?(Ambiguity) -%>
        <dt><%= f.label :type, 'Fandom(s)'.t %></dt>
        <dd><%=h 'Add Fandom:'.t %>&nbsp;<%= select("fandom", 'fandom_name', (Fandom.canonical - @tag.family).collect {|p| [ p.name, p.name ] }, { :include_blank => true }) %><br />
        <%- unless @tag.fandoms.blank? -%>
          <ul class="tag-edit-items">
          <% @tag.fandoms.each do |tag| %>
            <li><%= check_box_tag("fandoms[]", tag.name, true, :id => "tag_fandom_#{tag.id}") %><%= f.label "fandom_#{tag.id}", link_to_edit_tag(tag) %></li>
          <%- end -%>
          </ul>
        <%- end -%>
        </dd>
      <%- end -%>

      <%- if @tag.canonical? %>

        <% if @tag.is_a?(Fandom) || @tag.is_a?(Pairing) %>
          <dt><%= f.label :type, 'Character(s)'.t %></dt>
          <dd><%=h 'Add Character:'.t %>&nbsp;<%= select("character", 'character_name', (Character.canonical - @tag.family).collect {|p| [ p.name, p.name ] }, { :include_blank => true }) %><br />
          <%- unless @tag.characters.blank? -%>
          <ul class="tag-edit-items">
          <% @tag.characters.each do |tag| %>
          <li><%= check_box_tag("characters[]", tag.name, true, :id => "tag_character_#{tag.id}") %><%= f.label "character_#{tag.id}", link_to_edit_tag(tag) %></li>
          <%- end %>
          </ul>
          <%- end %>
          </dd>
        <%- end %>

        <% if @tag.is_a?(Fandom) || @tag.is_a?(Character) %>
          <dt><%= f.label :type, 'Pairings(s)'.t %></dt>
          <dd><%=h 'Add Pairing:'.t %>&nbsp;<%= select("pairing", 'pairing_name', (Pairing.canonical - @tag.family).collect {|p| [ p.name, p.name ] }, { :include_blank => true }) %><br />
          <%- unless @tag.pairings.blank? -%>
            <ul class="tag-edit-items">
            <% @tag.pairings.each do |tag| %>
              <li><%= check_box_tag("pairings[]", tag.name, true, :id => "tag_pairing_#{tag.id}") %><%= f.label "pairing_#{tag.id}", link_to_edit_tag(tag) %></li>
            <%- end %>
            </ul>
          <%- end %>
          </dd>
        <%- end %>

        <%- if @tag.is_a?(Fandom) %>
          <dt><%= f.label :type, 'Freeforms'.t %></dt>
          <dd><%=h 'Add Freeform:'.t %>&nbsp;<%= select("freeform", 'freeform_name', (Freeform.canonical - @tag.family).collect {|p| [ p.name, p.name ] }, { :include_blank => true }) %><br />
          <%- unless @tag.freeforms.blank? -%>
            <ul class="tag-edit-items">
            <% @tag.freeforms.each do |tag| %>
              <li><%= check_box_tag("freeforms[]", tag.name, true, :id => "tag_freeform_#{tag.id}") %><%= f.label "freeform_#{tag.id}", link_to_edit_tag(tag) %></li>
            <%- end %>
            </ul>
          <%- end %>
          </dd>
        <%- end -%>

        <%- if @tag.is_a?(Ambiguity) %>
          <dt><%= f.label :type, 'Disambiguators'.t %>:</dt>
          <dd><%=h 'Add Disambiguator:'.t %>&nbsp;<%= select("disambiguator", 'disambiguator_name', (@tag.find_similar - @tag.disambiguators - [@tag]).collect {|p| [ description(p), p.name ] }, { :include_blank => true }) %><br />
          <%- unless @tag.disambiguators.blank? -%>
            <ul class="tag-edit-items">
            <% @tag.disambiguators.each do |tag| %>
              <li><%= check_box_tag("disambiguators[]", tag.name, true, :id => "tag_disambiguator_#{tag.id}") %><%= f.label "disambiguator_#{tag.id}", link_to_edit_tag(tag) %></li>
            <%- end %>
            </ul>
          <%- end %>
          </dd>
        <%- end %>

        <dt><%= f.label :type, 'Synonyms'.t %></dt>
        <dd><%=h 'Add Synonym:'.t %>&nbsp;<%= select("synonym", 'synonym_name', (@tag.class.nonsynonymous - @tag.family).collect {|p| [ p.name, p.name ] }, { :include_blank => true }) %><br />
        <%- unless @tag.mergers.blank? -%>
          <ul class="tag-edit-items">
          <% @tag.mergers.each do |tag| %>
            <li><%= check_box_tag("synonyms[]", tag.name, true, :id => "tag_synonym_#{tag.id}") %><%= f.label "synonym_#{tag.id}", link_to_edit_tag(tag) %></li>
          <%- end %>
          </ul>
        <%- end -%>

      <%- else %>
        <dt><%= f.label :type, 'Synonym of'.t %></dt>
        <% if @tag.merger.is_a? Tag %>
          <dd><%= check_box_tag("keep_synonym", @tag.merger.name, true, :id => "tag_keep_synonym") %><%= f.label :keep_synonym, link_to_edit_tag(@tag.merger) %></dd>
        <%- else %>
          <dd><%= f.select('merger_id', (@tag.class.canonical - [@tag]).collect {|p| [ p.name, p.id ]}, { :include_blank => true, :selected => nil }) %></dd>
          <dt><%= label :new_synonym, 'Rename'.t %>*:</dt>
          <dd><%= text_field_tag :new_synonym %><br />
          <%=h '*This text box will do one of two things:'.t %> <%=h '1. correct the capitalization of the current tag.'.t %> <%=h 'OR 2. Create a canonical tag, to which the current tag is then made a synonym.'.t %></dd>
        <%- end %>
      <%- end %>
      </dd>

    <% end %>
  </dl>
  <p class="submit">
  <%= submit_tag "Save changes".t %>
  </p>
<%- end -%>


<%= tag_wrangler_footer %>

